import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class CMLConverter
{
    public static void URLSource(String url) 
    {
        try {
            URL google = new URL(url);
            URLConnection yc = google.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc
                        .getInputStream()));
            String inputLine;
            int t = 1;
            while ((inputLine = in.readLine()) != null) {
                if(t%2 != 0)
                {
                    for(int i = 1; i < inputLine.length(); i+=2)
                    {
                        System.out.print(inputLine.substring(i,i+1));
                    }
                    System.out.println();
                }
                t++;
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    

    public static String convertByte(String b)
    {
        String[] bytes = {"A","B","C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "<", ">", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " ", ".", ",", "!", "'"};
        String[] hex = {"41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "6B", "6C", "83", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "29", "3A", "2B", "2D", "AE"};

        for(int t = 0; t < bytes.length; t++)
        {
            if (bytes[t].equals(b))
                return hex[t];
        }
        return "00";
    }

    public static void main(String[] args) throws IOException
    {     
        URLSource("http://www.ticalc.org/cgi-bin/zipview?83plus/asm/games/obstacle_snake.zip;readme.txt");
        System.out.println("---Seperator---");
        URLSource("http://www.ticalc.org/cgi-bin/zipview?83plus/basic/games/puzzle/sliding/drifter.zip;Read%20Me.txt");

        Scanner in = new Scanner(System.in);
        System.out.print("Name of htm/html to convert: ");
        String webPage = in.nextLine();

        System.out.print("Name of output: ");
        String cmlPage = in.nextLine();

        System.out.print("CML Page Title: ");
        String cmlTitle = in.nextLine();

        System.out.print("Will you have a favicon? ");
        String yesOrNo = in.nextLine();

        if(yesOrNo.equalsIgnoreCase("yes"))
        {
            System.out.print("Input hex code for 5x5 icon (10 digits): ");
            String cmlFav = in.nextLine();
        }
        else
        {
            String cmlFav = "DEF";
        }

        File fileName = new File(webPage);
        String line = "";
        Scanner inFile = new Scanner(fileName);

        String data = "";
        String dataBin = "";
        int lineNum = 0;
        while (inFile.hasNext())
        {
            line = inFile.nextLine( );
            for(int i = 0; i < line.length(); i++)
            {
                if(line.substring(i, i+1).equals("h") && i < line.length()-4)
                    if(line.substring(i, i+4).equals("html"))
                        line = line.substring(0, i) + "cml" + line.substring(i+4);
            }
            line = line.toUpperCase();
            int t = 0;
            line += " ";
            for(int i = 0; i < line.length() - 1; i++)
            {
                t = 0;
                data += convertByte(line.substring(i, i+1));
                if(line.substring(i, i+1).equals(">"))
                {
                    data += "3F";
                    t = 1;
                }
                if(line.substring(i+1, i+2).equals("<"))
                {
                    data += "3F";
                    t = 0;
                }
            }
            if(t == 0)
                data += "3F";
        }
        inFile.close();

        data = data.substring(0, data.length()-2);

        data += "          ";
        for(int i = 0; i < data.length() - 11; i+=2)
        {
            dataBin += data.substring(i, i+2);
            if(data.substring(i, i+2).equals("3F") && data.substring(i+2, i+4).equals("3F"))
                i += 2;
        }

        String url = "http://xeverion.com/Tools/Abyte/Convert?v=";

        String url2 = (url + cmlPage + ",A CML File," + dataBin);
        BareBonesBrowserLaunch.openURL(url2);
    }//end of main method
}//end of class